#include "Seqlist.h"
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size_of = ps->capacity = 0;
}
void SLcheckCapacity(SL *ps)
{
	if (ps->capacity == ps->size_of)
	{
		int newcapcity = ps->capacity == 0 ? 4 : ps->capacity*2;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, sizeof(SLDataType) * newcapcity);
		if (tmp == NULL)
		{
			perror("relloc fail");
		}
		ps->arr = tmp;
		ps->capacity = newcapcity;
	}
}
void SLPushBack(SL* ps, SLDataType x)
{
	 assert(ps);
     SLcheckCapacity(ps);
	ps->arr[ps->size_of++] = x;
}
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLcheckCapacity(ps);
	int i = 0;
	for (i = ps->size_of++; i > 0; i--)
	{
		ps-> arr[i] = ps->arr[i - 1];
	}
	ps->arr[i] = x;
}
void SLPopBack(SL* ps)
{
	assert(ps && ps->size_of);
	--ps->size_of;
}
void SLPrintf(SL* ps)
{
	int i = 0;
	for (i = 0; i < ps->size_of; i++)
	{
		printf("%d", ps->arr[i]);

	}
	printf("\n");
}
void SLPopFront(SL* ps)
{
	assert(ps && ps->size_of);
	int i = 0;
	for (int i = 1; i < ps->size_of; i++)
	{
		ps->arr[i - 1] = ps->arr[i];
	}
	--ps->size_of;
}
int  SLFind(SL* ps, SLDataType x)
{
	int i = 0;
	for (i = 0; i < ps->size_of; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(pos>=0 && pos<=ps->size_of);
	SLcheckCapacity(ps);
	int i = 0; 
	for (int i = ps->size_of++; i > pos; i--)
	{
		ps->arr[i + 1] = ps->arr[i];
	}
}
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(ps >= 0 && pos < ps->size_of);
	int i = 0;
	for (int i = pos; i < ps->size_of-1; i++)
	{
		ps->arr[i] = ps->arr[i+1];
	}
	--ps->size_of;
}
void SLDestory(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
		ps->arr = NULL;
		ps->capacity = 0;
		ps->size_of = 0;
	
}